﻿using Core;
using Core.Data;
using Core.Domain.Directory;
using Core.Domain.Stores;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Services.Directory
{
    /// <summary>
    /// 城市服务实现类
    /// </summary>
    public partial class CitysService : ICitysService
    {
        #region Fields

        private readonly IRepository<City> _cityRepository;
        private readonly IStoreContext _storeContext;

        #endregion

        #region Ctor

        public CitysService(IRepository<City> stateProvinceRepository,
            IStoreContext storeContext)
        {
            this._storeContext = storeContext;
            this._cityRepository = stateProvinceRepository;
        }

        #endregion

        #region Methods
        /// <summary>
        /// 删除城市
        /// </summary>
        /// <param name="city"></param>
        public virtual void DeleteCity(City city)
        {
            if (city == null)
                throw new ArgumentNullException("city");

            _cityRepository.Delete(city);
        }
        /// <summary>
        /// 根据主键获取ID
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public virtual City GetCityById(int cityId)
        {
            if (cityId == 0)
                return null;

            return _cityRepository.GetById(cityId);
        }

        /// <summary>
        /// 根据省份Id获取城市集合
        /// </summary>
        /// <returns>State/province collection</returns>
        public virtual IList<City> GetCityByStateId(int stateId)
        {
            var query = from sp in _cityRepository.Table
                        orderby sp.DisplayOrder
                        where sp.StateId == stateId
                        select sp;
            var stateProvinces = query.ToList();
            return stateProvinces;
        }

        /// <summary>
        /// 获取所有城市
        /// </summary>
        /// <param name="showHidden"></param>
        /// <returns></returns>
        public virtual IPagedList<City> GetCities(string name = "", bool? hot = true, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _cityRepository.Table;

            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(e => e.Name.Contains(name));

            if (hot.HasValue)
                query = query.Where(e => e.IsHot == hot);

            query = query.OrderBy(e => e.DisplayOrder);

            return new PagedList<City>(query, pageIndex, pageSize);
        }

        /// <summary>
        /// 热点城市
        /// </summary>
        /// <param name="hot"></param>
        /// <returns></returns>
        public virtual IList<City> GetHotCities(bool hot = true)
        {
            var query = _cityRepository.Table;

            query = query.Where(e => e.IsHot == hot);

            query = query.OrderBy(e => e.DisplayOrder);

            return query.ToList();
        }
        /// <summary>
        ///  新增城市
        /// </summary>
        public virtual void InsertCity(City city)
        {
            if (city == null)
                throw new ArgumentNullException("city");

            _cityRepository.Insert(city);

        }

        /// <summary>
        /// 更新城市
        /// </summary>
        /// <param name="stateProvince">State/province</param>
        public virtual void UpdateCity(City city)
        {
            if (city == null)
                throw new ArgumentNullException("city");

            _cityRepository.Update(city);

        }

        #endregion
    }
}
